package soulkun.library.custom.view

import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import androidx.databinding.BindingAdapter
import soulkun.library.utils.R
import soulkun.library.utils.getAppResources
import soulkun.library.utils.toast
import kotlin.math.min
import kotlin.properties.Delegates

/**
 * 固定圆形带边框的ImageView
 * app:borderWidth —— 边框宽度 默认0dp
 * app:borderColor —— 边框颜色 默认白色
 */
class RoundBorderImageView : androidx.appcompat.widget.AppCompatImageView {

    private val heroPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val borderPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var mRadius = 0
    private lateinit var mBitmapShader: BitmapShader
    private var borderWidth = 0
    private var borderColor = Color.WHITE
    private var dataBindingBorderWidth: Int? = null
    private var dataBindingBorderColor: Int? = null

    companion object {
        //单向绑定
        @BindingAdapter("app:roundBorderWidth")
        @JvmStatic
        fun setRoundBorderWidth(view: RoundBorderImageView, params: Int) {
            if (view.dataBindingBorderWidth != params) {
                view.dataBindingBorderWidth = params
                view.invalidate()
            }
        }

        //单向绑定
        @BindingAdapter("app:roundBorderColor")
        @JvmStatic
        fun setRoundBorderColor(view: RoundBorderImageView, params: Int) {
            if (view.dataBindingBorderColor != params) {
                view.dataBindingBorderColor = params
                view.invalidate()
            }
        }
    }

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundBorderImageView)
        borderWidth =
            typedArray.getDimensionPixelSize(R.styleable.RoundBorderImageView_roundBorderWidth, 0)
        borderColor = typedArray.getColor(R.styleable.RoundBorderImageView_roundBorderColor, Color.WHITE)
        typedArray.recycle()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        min(measuredWidth, measuredHeight).let {
            mRadius = it / 2
            setMeasuredDimension(it, it)
        }
    }

    override fun onDraw(canvas: Canvas) {
        if (null == drawable) {
            return
        }
        setBitmapShader()
        canvas.drawCircle(
            mRadius.toFloat(),
            mRadius.toFloat(),
            mRadius.toFloat() - borderWidth,
            heroPaint
        )
        borderPaint.style = Paint.Style.STROKE
        borderPaint.strokeWidth = dataBindingBorderWidth?.toFloat() ?: borderWidth.toFloat()
        borderPaint.color = dataBindingBorderColor ?: borderColor
        canvas.drawCircle(
            mRadius.toFloat(),
            mRadius.toFloat(),
            mRadius.toFloat() - borderWidth,
            borderPaint
        )
    }

    /**
     * 设置BitmapShader
     */
    private fun setBitmapShader() {
        val drawable = drawable ?: return
        val bitmap = drawableToBitmap(drawable)
        mBitmapShader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
        heroPaint.shader = mBitmapShader
    }

    /**
     * drawable转bitmap
     *
     * @paramdrawable
     * @return
     */
    private fun drawableToBitmap(drawable: Drawable): Bitmap {
        if (drawable is BitmapDrawable) {
            return drawable.bitmap
        }
        val w = drawable.intrinsicWidth
        val h = drawable.intrinsicHeight
        val bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        drawable.setBounds(0, 0, w, h)
        drawable.draw(canvas)
        return bitmap
    }

}